package me.zhanshi123.invitelink;

import me.zhanshi123.jdbcconnectionbridge.JdbcConnectionBridge;

import java.sql.*;

public class Database {
    private Connection connection;

    public Database() {
        init();
    }

    private PreparedStatement getUser, insertAward, getAward;

    public void init() {
        try {
            connection = JdbcConnectionBridge.getConnectionPoolHandler().getConnection(Main.getConfigManager().getConnectionName());
            Statement statement = connection.createStatement();
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS `links` (\n" +
                    "`id`  int UNSIGNED NOT NULL AUTO_INCREMENT ,\n" +
                    "`user`  varchar(255) NOT NULL ,\n" +
                    "`ip`  varchar(255) NOT NULL ,\n" +
                    "PRIMARY KEY (`id`)\n" +
                    "INDEX `name` (`user`) USING BTREE " +
                    ")\n" +
                    ";");
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS `links_award` (\n" +
                    "`id`  int UNSIGNED NOT NULL AUTO_INCREMENT ,\n" +
                    "`name`  varchar(255) NOT NULL ,\n" +
                    "`award`  varchar(255) NOT NULL ,\n" +
                    "PRIMARY KEY (`id`)\n" +
                    "INDEX `name` (`name`) USING BTREE " +
                    ")\n" +
                    ";");
            statement.close();
            getUser = connection.prepareStatement("SELECT * FROM `links` WHERE `user` = ?;");
            insertAward = connection.prepareStatement("INSERT INTO `links_award` VALUES(NULL,?,?);");
            getAward = connection.prepareStatement("SELECT * FROM `links_award` WHERE `name` = ? AND `award`= ?;");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void checkConnection() {
        try {
            if (connection.isClosed()) {
                init();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void release() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int getSharedAmount(String name) {
        checkConnection();
        int amount = 0;
        try {
            getUser.setString(1, name);
            ResultSet resultSet = getUser.executeQuery();
            while (resultSet.next()) {
                amount++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return amount;
    }

    public boolean isAwarded(String name, int amount) {
        checkConnection();
        try {
            getAward.setString(1, name);
            getAward.setInt(2, amount);
            ResultSet rs = getAward.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    public void claimAward(String name, int amount) {
        checkConnection();
        try {
            insertAward.setString(1, name);
            insertAward.setInt(2, amount);
            insertAward.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}